package cn.yunhe.action;

import cn.yunhe.entity.News;
import cn.yunhe.entity.Page;
import cn.yunhe.entity.User;
import cn.yunhe.service.INewsService;
import cn.yunhe.service.IUserService;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@ParentPackage("json-default")
public class UserAction extends BaseAction implements ModelDriven<Object> {

    private User user;

    private int pageNum;

    private int pageSize;

    private Page page;

    @Resource
    private IUserService userService;

    @Resource
    private INewsService newsService;


    @Action(value = "user_reg", results = {
            @Result(location = "/login.jsp")
    })
    public String userReg() {
        userService.addUser(user);
        return SUCCESS;
    }

    @Action(value = "user_login", results = {
            @Result(type = "json")}, params = {"contentType", "text/html"})
    public String userLogin() throws IOException {
        User u = userService.loginUser(user);
        getSession().setAttribute("user", u);
        page = newsService.getNewsByPage(pageNum, pageSize);

        String responseStr = JSONObject.toJSONString(page);
        System.out.println("page====" + responseStr);


        HttpServletResponse response = getResponse();

        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("text/html;charset=utf-8");

        PrintWriter out = response.getWriter();

        out.print("返回======" + responseStr);
        out.flush();


        return SUCCESS;

    }


    public IUserService getUserService() {
        return userService;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public INewsService getNewsService() {
        return newsService;
    }

    public void setNewsService(INewsService newsService) {
        this.newsService = newsService;
    }

    @Override
    public User getModel() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }
}
